<template>
    <view class="select-role">
		<view class="tips">
			<text>欢迎来到晓德助手</text><br>
			<text>{{loginTips}}</text>
            <view class="login-tips">
                <!-- <text>正在登录中</text> -->
                <image src="/static/loginLoading.gif" mode="aspectFit"></image>
            </view>
		</view>
		<!-- <view class="role">
			<view class="role-item" @click="confirmBtn(1)">
				<image src="/static/tea.png" mode="aspectFit"></image>
				<text>教师</text>
			</view>
			<view class="role-item" @click="confirmBtn(2)">
				<image src="/static/stu.png" mode="aspectFit"></image>
				<text>学生</text>
			</view>
		</view> -->
    </view>
</template>
<script>
export default {
    data() {
        return {
			loginTips: ''	,
            flag: true,
        }
    },
    onLoad(){
        const tokenObj = uni.getStorageSync('tokenObj');
        // if(tokenObj&&tokenObj.token){
        //     this.api.get('user/getMeInfo').then(res=>{
        //         let info = res.data.user
        //         getApp().globalData.userInfo = info;
        //         if(info.user_type == 1){
        //             uni.switchTab({
        //                 url: '/pages/teachers/index'
        //             })
        //         }else{
        //             uni.switchTab({
        //                 url: '/pages/student/task-page'
        //             })
        //         }
                
        //     })
        // }
        let code = getUrlParam(location.href).code||'3CBF3E92FA45BADB3FC4BBB0CCB080085E1FA13ECFEB13037ABAE2A83A5AAC9250026DAF14EA43B7D138E77C6CAB4F228E51628C54B550E73FF71356C907E1227E77D62BA71F6E8231C03CCC4A62914F4FFEED551EB87A272BC338DA385FC87FE136B6324D0AAC7E7B5671C0B91A3DD1E245CEF989E69F86807311C7BC4A6451AEBE5052555EF8D25052B9D0ABA3D0E793C85E08768511C6A3A550816FFFA50DC351ADA36457F83078393C3C721B9EACD27B2F2C88ABF731F6F0A46CFC291C59CA91122A13628835065461203800E4C0D440EB'
        this.WElinkLogin(code)
        
        
    },
    onShow(){
        
        
    },
    methods: {
        WElinkLogin(code){
            if(!code) {
                this.loginTips = '缺少基座参数'
                return}
            this.api.post('user/login', {
				code: code,
                platform: 2
			}).then(res=>{
                let data = res.data;
                this.flag = true
                if(data.state != 'ok'){
                    this.loginTips = data.errorMsg
                }else{
                    if(data.banDingCode){
                        this.utils.showToast('请先绑定WElink')
                        setTimeout(()=>{
                            this.confirmBtn(data.banDingCode) 
                        }, 1000)
                        return
                    }
                    this.userLogin(data)
                }
                
            })
        },
        userLogin(dataInfo){
            let info = dataInfo.user
            let item = dataInfo.token, wsToken = info.wsToken || '';
            getApp().globalData.isStudent = info.user_type==2?true:false
            let tokenObj = {
                userType: info.user_type,
                token: item,
                wsToken: wsToken,
                userId: info.id,// 使用用户id
                accountId: info.accountId
            }
            if(this.rememberPassword!=1){
                tokenObj.loginTime =  new Date().getTime()
            }
            uni.setStorageSync('tokenObj', tokenObj);
            uniRequest.defaults.headers.common['Token'] = item;
            // 判断添加账号
            let index = this.accounts.indexOf(this.account);
            if(index==-1){
                this.accounts.unshift(this.account);
            }else{
                this.accounts.splice(index,1);
                this.accounts.unshift(this.account);
            }
            
            getApp().globalData.userInfo = info;
            //保存账号
            uni.setStorageSync('uAccounts', this.accounts);
            this.getfindMembers(dataInfo.usertype)
            
            //触发登陆事件
            uni.$emit('login',{});
        },
        confirmBtn(val){
			uni.reLaunch({
                url: '/pages/index/login?bdCode='+val
            })
		},
    }
}
function getUrlParam(sUrl) {
	let indexStart = sUrl.indexOf('?')
	if(indexStart==-1){return {}};
	let k  = sUrl.slice(indexStart+1);
	let x = k.replace(/&/g, '","').replace(/=/g, '":"');
	let y = '{"' + x + '"}';
	
	return JSON.parse(y);
}
</script>
<style lang="less">
    .select-role{
        overflow: hidden;
        display: flex;
		flex-direction: column;
		justify-content: center;
        height: calc(100vh - 100rpx);
        .tips{
			font-size: 32rpx;
			line-height: 60rpx;
			text-align: center;
        }
		.role-item{
			margin: 40rpx auto 0;
			width: 608rpx;
			height: 290rpx;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;
			box-shadow: 0px 2rpx 4rpx rgba(0, 0, 0, .35);
			image{
				width: 202rpx;
				height: 202rpx;
			}
			text{
				font-size: 64rpx;
				font-weight: 700;
			}
		}
        .radio2{
            margin-left: 48rpx;
        }
        
        .btn{
            text-align: center;
            margin: 80rpx 32rpx 0;
            padding: 10rpx 0;
            background-color: #8FE1EC;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #FFFFFF;
            font-size: 32rpx;
            border-radius: 8rpx;
        }
    }
    .login-tips{
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        image{
            width: 300rpx;
            height: 300rpx;
        }
    }
</style>